//
//  Town.swift
//  Town
//
//  Created by 左忠飞 on 2021/8/4.
//

import Foundation

struct Town {
    let region:String
    var population:Int
    var numberOfStoplights:Int
    
    //自定义的成员初始化方法
    //如果写了自定义的成员初始化方法,就不会提供默认的成员初始化方法了
//    init(region:String,population:Int,stoplights:Int){
//        self.region = region
//        self.population = population
//        numberOfStoplights = stoplights
//    }
    
    //可失败的初始化方法
    //在init后边加一个?
    //可能会返回nil
    init?(region:String,population:Int,stoplights:Int){
        guard population > 0 else {
            return nil
        }
        self.region = region
        self.population = population
        numberOfStoplights = stoplights
    }
    
    //委托初始化方法
    //委托初始化方法可以包含对该类型其他初始化方法的调用
    init?(population:Int,stoplights:Int){
        self.init(region: "N/A", population: population, stoplights: stoplights)
    }
    
    //嵌套类型
    enum Size {
        case small
        case medium
        case large
    }
    
    //懒加载属性
    lazy var townSize:Size = {
        switch self.population {
        case 0...10_000:
            return .small
            
        case 10_000...100_000:
            return .medium
            
        default:
            return .large
        }
    }()
    
    
    //计算属性
    var townSize2:Size {
        get{
            switch self.population {
            case 0...10_000:
                return .small
                
            case 10_000...100_000:
                return .medium
                
            default:
                return .large
            }
        }
    }
    
    
    
    
    
    func printDescription(){
        print("region: \(region),Pupulation: \(population),number of stoplights: \(numberOfStoplights)")

    }
    
    //如果结构体的实例方法要修改结构体的属性,必须标记为mutating
    mutating func changePopulation(by amount:Int){
        population += amount
    }
}
